import React, { Component,PropTypes } from 'react';
import {Link} from 'react-router';
import InformationComponent from '../../Component/OrderManage/InformationComponent';
import Top from '../CommonComponent/Top'
import Empty from '../../Component/CommonComponent/Empty';
import CommonBtn from '../../Component/CommonComponent/CommonBtn';
import {AddressList,SetAddress} from '../../Action/auth'

export default class ChooseInfomation extends Component {

    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            addressList : []
        };
      }

    static contextTypes = {
        router:PropTypes.object
    }

    async componentWillMount() {
        await this.getAddressList()
    }

    async getAddressList(){
       await AddressList()
        .then(res=>{
            this.setState({addressList:res.resultList})
        })
        .catch(err=>{
            console.warn('获取地址列表错误',err)
        })
    }

    async choosePath(value){
        //直接购买
        console.log('this.props.location.query.path',this.props.location.query.path)
        if(this.props.location.query.path){
            //RPC.emit('choosePath',value,this.props.location.query.orderNo)
            await this.SetAddressOrder(this.props.location.query.orderArrays,value.id)
            sessionStorage.setItem('getAddress',value.address)
            sessionStorage.setItem('getDetail',value.detail)
            sessionStorage.setItem('getName',value.name)
            sessionStorage.setItem('getMobile',value.mobile)
            this.context.router.push({pathname:'/comfirmPayMoney',
                query:{
                    orderId:this.props.location.query.orderNo,
                    isRedirectCh:true
                }});

        }
        //订单进来的
        if(this.props.location.query.orderPath){
            await this.SetAddressOrder(this.props.location.query.orderId,value.id)
            sessionStorage.setItem('getOrderAddress',value.address)
            sessionStorage.setItem('getOrderDetail',value.detail)
            sessionStorage.setItem('getOrderName',value.name)
            sessionStorage.setItem('getOrderMobile',value.mobile)
            this.context.router.push({pathname:'/orderDetails',
                query:{
                    orderId:this.props.location.query.orderId,
                    indexStatus:this.props.location.query.index,
                    //isToPay:this.props.location.query.isToPay,
                    isOrder:true
                }});

        }

        //库存取货的
        if(this.props.location.query.stock){
            await this.SetAddressOrder(this.props.location.query.orderId,value.id)
            sessionStorage.setItem('getOrderAddress',value.address)
            sessionStorage.setItem('getOrderDetail',value.detail)
            sessionStorage.setItem('getOrderName',value.name)
            sessionStorage.setItem('getOrderMobile',value.mobile)
            this.context.router.push({pathname:'/myStock',
                query:{
                    address:value.address,
                    detail:value.detail,
                    getAdd:true,
                    area:value.id
                }});

        }

    }

    async SetAddressOrder(orderNos,addressId){
        await SetAddress(orderNos,addressId)
            .then(res=>{
                const {resultList} = res
                this.setState({addressList:resultList})
            })
            .catch(err=>{
                console.warn('获取地址列表错误',err)
            })
    }

    render() {
        const {addressList} = this.state
        return (
            <div>
                <Top
                    title = {'地址管理'}
                />
                <div className="containerNav" style={{top:50}}>

                    {
                        addressList == ''||addressList == null?
                            <Empty />
                            :
                            addressList&&addressList.map((el,index)=>{
                            return (
                                <InformationComponent
                                    name={el.name}
                                    phone={el.mobile}
                                    path={(el.address?el.address:'')+(el.detail?el.detail:'')}
                                    onClick = {()=>this.choosePath(el)}
                                />
                            )
                        })
                    }
                    <div className="flex pf width100" style={{bottom:40}}>
                        <Link to="/deliveredInformation" className="flex-1">
                            <CommonBtn
                                title={'添加地址'}
                            />
                        </Link>
                        {
                            addressList == ''?
                                null:
                                <Link to="/manageInformation" className="flex-1">
                                    <CommonBtn
                                        title={'管理'}
                                    />
                                </Link>
                        }

                    </div>
                </div>
            </div>
        );
    }
}
